﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SZ.Zmotion
{
    public class ZMotionHelper
    {
        private string _ip;
        public ZMotionHelper(string ip = "")
        {
            _ip = ip;
        }

        /// <summary>
        /// 句柄，指针用于后续操作
        /// </summary>
        private IntPtr g_handle = IntPtr.Zero;

        public bool IsConnected { get; set; } = false;

        /// <summary>
        /// 连接到指定的IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                int err = zmcaux.ZAux_OpenEth(this._ip, out g_handle);
                IsConnected = err == 0 && g_handle != IntPtr.Zero;
            }
            catch (Exception)
            {
                IsConnected = false;
            }
            return IsConnected;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void Close()
        {
            int err = zmcaux.ZAux_Close(g_handle);
            if (err == 0)
            {
                g_handle = IntPtr.Zero;
            }
        }

        /// <summary>
        /// 获取板卡IP 列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetBoardIPList()
        {
            List<string> ipList = new List<string>();
            // 假设这里有逻辑来获取板卡的IP地址
            // 例如：ipList.Add("
            StringBuilder sb = new StringBuilder(200);

            var error = zmcaux.ZAux_SearchEthlist(sb, 200, 10);
            if (error == 0)
            {
                string result = sb.ToString().Trim();
                if (result.Contains(' '))
                {
                    ipList = result.Split(' ').ToList();
                }
                else
                {
                    ipList.Add(result);
                }
            }
            else
            {
                return new List<string> { "Error: " + error.ToString() };
            }

            return ipList;
        }

        int unit0, unit1, unit2, unit3 = 400;

        /// <summary>
        /// 连续运动
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="vel">速度</param>
        /// <param name="dir">方向</param>
        /// <param name="velMin">最小速度</param>
        /// <param name="acc">加速度</param>
        /// <param name="dec">减速度</param>
        /// <param name="sramp">平滑时间ms</param>
        /// <returns></returns>
        public bool VMove(short axis, float vel, bool dir, float velMin, float acc, float dec, float sramp)
        {
            try
            {
                // 设置轴参数
                zmcaux.ZAux_Direct_SetAtype(g_handle, axis, 1);

                switch (axis)
                {
                    case 0:
                        zmcaux.ZAux_Direct_SetUnits(g_handle, axis, unit0);
                        break;
                    case 1:
                        zmcaux.ZAux_Direct_SetUnits(g_handle, axis, unit1);
                        break;
                    case 2:
                        zmcaux.ZAux_Direct_SetUnits(g_handle, axis, unit2);
                        break;
                    case 3:
                        zmcaux.ZAux_Direct_SetUnits(g_handle, axis, unit3);
                        break;
                    default:
                        break;
                }

                zmcaux.ZAux_Direct_SetLspeed(g_handle, axis, velMin);
                zmcaux.ZAux_Direct_SetSpeed(g_handle, axis, vel);
                zmcaux.ZAux_Direct_SetAccel(g_handle, axis, acc);
                zmcaux.ZAux_Direct_SetDecel(g_handle, axis, dec);
                zmcaux.ZAux_Direct_SetSramp(g_handle, axis, sramp);
                zmcaux.ZAux_Direct_Single_Vmove(g_handle, axis, dir ? 1 : -1);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool StopAsix(int axis)
        {
            bool isOk = false;
            try
            {
                int err = zmcaux.ZAux_Direct_Single_Cancel(g_handle, axis, 2);
                isOk = (err == 0);
            }
            catch (Exception)
            {
                isOk = false;
            }
            return isOk;
        }
    }
}
